let HttpUtils = {
    contentType: 'application/x-www-form-urlencoded',
    post: function (url, params, callBackFunction) {
        ajax(url, 'POST', false, params, callBackFunction, this.contentType);
    },
    post_async: function (url, params, callBackFunction) {
        ajax(url, 'POST', true, params, callBackFunction, this.contentType);
    },
    post_json: function (url, params, callBackFuntion) {
        ajax(url, 'POST', false, params, callBackFuntion, 'application/json;charset=utf-8');
    },
    post_json_async: function (url, params, callBackFuntion) {
        ajax(url, 'POST', true, params, callBackFuntion, 'application/json;charset=utf-8');
    },
    get_async: function (url, callBackFunction) {
        ajax(url, 'GET', true, null, callBackFunction, this.contentType);
    },
    get: function (url, callBackFunction) {
        ajax(url, 'GET', false, null, callBackFunction, this.contentType);
    },
    form_self: function (url, params) {
        let basePath = $('#path').val();
        form_submit(basePath + url, params, '_self');
    },
    download: function (url, param) {
        let basePath = $('#path').val();
        downloadFile(basePath + url, param);
    },
    upload_file(url, formData, callback) {
        $.ajax({
            url: url,
            type: "POST",
            data: formData,
            processData: false,
            dataType: "json",
            async: false,
            cache: false,
            contentType: false,
            success: function (data) {
                callback(data);
            }
        });
    }

};


function ajax(url, method, async, param, callback, contentType) {
    let basePath = $('#path').val();
    $.ajax({
        url: basePath + url,
        type: method,
        data: param,
        async: async,
        headers: {
            'Content-type': contentType
        },
        success: function (result) {
            if (isNull(result) && result.code === 40003) {
                wrong_msg("没有权限");
                return false;
            }
            if (isNull(result) || result.code !== 2000) {
                console.error("请求结果出现错误：", result.msg);
                wrong_msg(result.msg);
                return false;
            }

            if (callback !== null) {
                callback(result);
            }
            return true;
        },
        error: function (result) {
            console.error("请求结果出现错误：", result);
            return false;
        }
    });
}

function form_submit(URL, params, target) {
    let temp_form = document.createElement("form");
    temp_form.action = URL;
    //如需打开新窗口，form的target属性要设置为'_blank'
    temp_form.target = target;
    temp_form.method = "post";
    temp_form.style.display = "none";
    //添加参数
    for (let item in params) {
        let opt = document.createElement("input");
        opt.name = item;
        opt.value = isNull(params[item]) ? null : params[item];
        temp_form.appendChild(opt);
    }
    document.body.appendChild(temp_form);
    temp_form.submit();
}

function downloadFile(url, params) {
    let frameElement = document.createElement('iframe');
    frameElement.style.display = 'none';
    frameElement.id = 'responseResult';
    frameElement.name = 'responseResult';
    frameElement.src = '';

    let temp_form = document.createElement("form");
    temp_form.action = url;
    temp_form.target = "responseResult";
    temp_form.method = "POST";
    temp_form.style.display = "none";
    for (let x in params) {
        let opt = document.createElement("input");
        opt.name = x;
        opt.value = params[x];
        temp_form.appendChild(opt);
    }

    document.body.appendChild(frameElement);
    frameElement.contentWindow.document.body.appendChild(temp_form);

    temp_form.submit();

    $("#responseResult").on("load", function () {
        let responseText = $("iframe")[0].contentDocument.body.getElementsByTagName("pre")[0].innerText;
        let result = JSON.parse(responseText);
        if (result.code !== "200") {
            duia_layer.msg(result.msg);
            console.log(result.msg);
            frameElement.remove();
        }
    })
}